export function fileLinkToStreamDownload(url, fileName, type) {
    let xhr = new XMLHttpRequest();
    xhr.open("get", url, true);
    xhr.setRequestHeader("Content-Type", `application/${type}`);
    xhr.responseType = "blob";
    xhr.onload = function () {
        if (this.status == 200) {
            //接受二进制文件流
            var blob = this.response;
            downloadExportFile(blob, fileName, type);
        }
    };
    xhr.send();
}

export function downloadExportFile(blob, fileName, fileType) {
    let downloadElement = document.createElement("a");
    let href = blob;
    if (typeof blob == "string") {
        downloadElement.target = "_blank";
    } else {
        href = window.URL.createObjectURL(blob); //创建下载的链接
    }
    downloadElement.href = href;
    downloadElement.download = fileName;
    document.body.appendChild(downloadElement);
    downloadElement.click(); //点击下载
    document.body.removeChild(downloadElement); //下载完成移除元素
    if (typeof blob != "string") {
        window.URL.revokeObjectURL(href); //释放掉blob对象
    }
}

export const customEmail = (list, form) => {
    let content = "";
    list.forEach((item) => {
        let label = "";
        if (item.type != "img") {
            if (item.text) {
                if (item.model && form[item.model]) {
                    label = `${item.text}:${form[item.model]}\n`;
                } else {
                    label = `${item.text}:\n`;
                }
            } else {
                if (item.placeholder) {
                    if (item.model && form[item.model]) {
                        label = `${item.placeholder}:${form[item.model]}\n`;
                    } else {
                        label = `${item.placeholder}:\n`;
                    }
                } else {
                    if (item.model && form[item.model]) {
                        label = `${form[item.model]}\n`;
                    }
                }
            }
        }
        content = content + label;
    });
    return content;
};
